package com.grapro.dao.Impl;

import com.grapro.dao.UserDao;
import util.BaseDao;

import java.sql.SQLException;
import java.util.List;
import java.util.Map;

public class UserDaoImpl extends BaseDao implements UserDao {


    @Override
    public Map<String, Object> loginCheck(int id) throws SQLException {
        String sql = "select password from oasystem.user where id = ?";
        Map<String, Object> map = super.queryMap(sql, id);
        return map;
    }

    @Override
    public Map<String, Object> getUserInfo(int id) throws SQLException {
        String sql = "select id, name, gender, password, roleName, deptName, jobName, telephone, address, statusName " +
                "from oasystem.user " +
                "left join oasystem.role on user.roleId = role.roleId " +
                "left join oasystem.dept on user.deptId = dept.deptId " +
                "left join oasystem.job on user.jobId = job.jobId " +
                "left join oasystem.applyStatus on user.statusId = applyStatus.statusId " +
                "where id = ? ";
        return super.queryMap(sql, id);
    }
    @Override
    public boolean updatePassword(int id, String newPassword1, String oldPassword) throws SQLException {
        String sql = "update oasystem.user set password = ? where id = ? and password= ?";
        return super.update(sql, newPassword1, id, oldPassword) > 0;

    }

    @Override
    public boolean clockInInsert(String[] arr, int userId) throws SQLException {
        String sql = "insert into oasystem.attendance (putIn, InTime, userId) values (?,?,?)";
        return super.update(sql, arr[0], arr[1], userId) > 0;
    }

    @Override
    public boolean clockOUtInsert(String[] arr, int user) throws SQLException {
        String sql = "insert into oasystem.attendance (putOut, outTime, userId) values (?,?,?)";
        return super.update(sql, arr[0], arr[1], user) > 0;
    }

    @Override
    public boolean clockOUtInsertByIn(String[] arr, int timeId) throws SQLException {
        String sql = "update oasystem.attendance set putOut = ?, outTime = ? where timeId = ?";
        return super.update(sql, arr[0], arr[1], timeId) > 0;
    }

    @Override
    public Map<String, Object> getUserId(String username) throws SQLException {
        String sql = "select id from oasystem.user where name = ?";
        return super.queryMap(sql, username);
    }

    @Override
    public Map<String, Object> getTimeId(String time, int userId) throws SQLException {
        String sql = "select timeId from oasystem.attendance where putIn = ? and userId = ? ";
        return super.queryMap(sql, time, userId);
    }

    @Override
    public boolean addUser(Map<String, String[]> parameterMap) throws SQLException {

        // 在这里不用给密码吗？
        String sql = "insert into oasystem.user" +
                "(name, gender, roleId, deptId, jobId, telephone, address) " +
                "values (?,?,?,?,?,?,?)";
        return super.update(sql, parameterMap.get("name")[0],
                Integer.parseInt(parameterMap.get("gender")[0]),
                Integer.parseInt(parameterMap.get("role")[0]),
                Integer.parseInt(parameterMap.get("dept")[0]),
                parameterMap.get("job")[0],
                parameterMap.get("telephone")[0],
                parameterMap.get("address")[0]) > 0;


    }
    @Override
    public boolean insertLO(Map<String, String[]> parameterMap, int userId) throws SQLException {

        String sql = "insert into oasystem.leaveandovertime(loTime, leaveOrOvertime, userId, cause) values (?,?,?,?)";

        return super.update(sql, parameterMap.get("loTime")[0], parameterMap.get("statusLo")[0], userId, parameterMap.get("cause")[0]) > 0;

    }

    @Override
    public List<Map<String, Object>> LeaveAndOvertime(int leaveOrOvertimeStatus) throws SQLException {
        String sql = "select id ,name, loTime, leaveOrOvertime, cause, statusName from oasystem.user " +
                "left join oasystem.leaveandovertime on leaveandovertime.userId = user.id " +
                "left join oasystem.applyststus on leaveandovertime.applyStatus = applyststus.statusId " +
                "where applyStatus = ? ";
        return super.queryMapList(sql, leaveOrOvertimeStatus);
    }




}
